<template>
  <div>
    <vxe-modal ref="xModal" v-model="isShowAddOrUpdateEmployeeModal" title="编辑&保存" width="1000" :zIndex="999" resize destroy-on-close>
      <vxe-form ref="xForm" :data="formData" :rules="formRules" title-align="right" title-width="100" @submit="submitEvent">
        <vxe-form-item title="客户信息" span="24" title-align="left" title-width="200px" :title-prefix="{icon: 'fa fa-address-card-o'}"></vxe-form-item>
        <vxe-form-item title="客户" field="customerId" span="12" :item-render="{name: 'Select', attrs: {disabled: isShowDetail}, props: {filterable: true}, options: customerList}"></vxe-form-item>
        <vxe-form-item title="客户全称" field="name" span="12" :item-render="{name: 'input', attrs: inputAttrs}"></vxe-form-item>
        <vxe-form-item title="联系信息" field="specification" span="12" :item-render="{name: 'input', attrs: inputAttrs}"></vxe-form-item>
        <vxe-form-item title="联系地址" field="specNo" span="12" :item-render="{name: 'input', attrs: inputAttrs}"></vxe-form-item>
        <vxe-form-item title="传真号码" field="unit" span="12" :item-render="{name: 'input', attrs: inputAttrs}"></vxe-form-item>

        <vxe-form-item title="产品信息" span="24" title-align="left" title-width="200px" :title-prefix="{icon: 'fa fa-address-card-o'}"></vxe-form-item>


        <vxe-form-item title="付款及其它信息" span="24" title-align="left" title-width="200px" :title-prefix="{icon: 'fa fa-address-card-o'}"></vxe-form-item>
        <vxe-form-item title="下单日期" field="orderDate" span="12" :item-render="{name: 'input', attrs: inputAttrs}"></vxe-form-item>
        <vxe-form-item title="订单编号" field="orderNo" span="12" :item-render="{name: 'input', attrs: inputAttrs}"></vxe-form-item>
        <vxe-form-item title="付款方式" field="payType" span="12" :item-render="{name: 'Select', attrs: inputAttrs, options: []}"></vxe-form-item>
        <vxe-form-item title="付款条件" field="payCondition" span="12" :item-render="{name: 'Select', attrs: inputAttrs, options: []}"></vxe-form-item>
        <vxe-form-item title="交货地点" field="deliveryPlace" span="12" :item-render="{name: 'input', attrs: inputAttrs}"></vxe-form-item>
        <vxe-form-item title="交货方式" field="deliveryType" span="12" :item-render="{name: 'input', attrs: inputAttrs}"></vxe-form-item>
        <vxe-form-item title="备注" field="remark" span="12" :item-render="{name: 'input', attrs: inputAttrs}"></vxe-form-item>
        <vxe-form-item title="内部备注" field="innerRemark" span="12" :item-render="{name: 'input', attrs: inputAttrs}"></vxe-form-item>

        <vxe-form-item align="center" span="24" :visible-method="isDetail">
          <vxe-button type="submit" status="primary">保存</vxe-button>
          <vxe-button type="reset">重置</vxe-button>
          <vxe-button @click="close()">取消</vxe-button>
        </vxe-form-item>
      </vxe-form>
    </vxe-modal>
  </div>
</template>
<script>
import { saleOrderApi } from '@/api/order/sale-order';
import { customerApi } from '@/api/customer/customer';
export default {
  name: 'SaleOrderTableAdd',
  components: {
  },
  props: {
  },
  data() {
    return {
      isShowAddOrUpdateEmployeeModal: false,
      isShowLoading: false,
      customerList: [],
      productList: [],
      isShowDetail: false,
      inputAttrs: {
        placeholder: '请输入', 
        disabled: false
      },
      formData: {
        id: 0,
        name: '',
        specification: '',
        specNo: '',
        unit: '',
        purchasePrice: 0,
        tags: [],
        remark: '',
        category: []
      },
      formRules: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        mobilePhone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^(13|14|15|16|17|18)\d{9}$/,
            message: '手机号格式错误',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  computed: {},
  watch: {
  },
  filters: {},
  created() {
    this.getCustomerList();
  },
  mounted() {
  },
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {},
  methods: {
    // 打开Modal
    showModal(row, isDetail) {
      if (isDetail !== undefined && isDetail) {
        this.isShowDetail = isDetail;
        this.inputAttrs.placeholder = '';
        this.inputAttrs.disabled = true;
      } else {
        this.isShowDetail = false;
        this.inputAttrs.placeholder = '请输入';
        this.inputAttrs.disabled = false;
      }
      if (row && Object.keys(row).length > 0) {
        this.formData = Object.assign({}, row);    
      } else {
        this.formData = {};
      }
      this.isShowAddOrUpdateEmployeeModal = true;
    },
    // isDetail
    isDetail({data, property}) {
      return !this.isShowDetail;
    },
    close() {
      this.$Modal.confirm({
        title: '友情提醒',
        content: '确定要关闭吗?关闭后表单会被重置!',
        onOk: () => {
          this.$refs.xModal.close();
        }
      });
    },
    // 点击提交
    submitEvent() {
      if (this.formData.id) {
        this.update(this.formData);
      } else {
        this.add(this.formData);
      }
    },
    // 修改成员
    async update(data) {
      try {
        let reqData = Object.assign({}, data);
        if (data.tags !== undefined) {
          reqData.tags = data.tags.join(',');
        }
        if (data.category !== undefined) {
          reqData.category = data.category[(data.category.length - 1)];
        }
        this.isShowLoading = true;
        let result = await saleOrderApi.update(reqData);
        this.isShowAddOrUpdateEmployeeModal = false;
        this.$Message.success('修改成功');
        this.$emit('addSuccess');
      } catch (e) {
        //TODO zhuoda sentry
        console.error(e);
      } finally {
        this.isShowLoading = false;
      }
    },
    // 添加新用户接口
    async add(data) {
      try {
        let reqData = Object.assign({}, data);
        if (data.tags !== undefined) {
          reqData.tags = data.tags.join(',');
        }
        if (data.category !== undefined) {
          reqData.category = data.category[(data.category.length - 1)];
        }
        this.isShowLoading = true;
        let result = await saleOrderApi.add(reqData);
        this.isShowAddOrUpdateEmployeeModal = false;
        this.$Message.success('添加成功');
        this.$emit('addSuccess');
      } catch (e) {
        //TODO zhuoda sentry
        console.error(e);
      } finally {
        this.isShowLoading = false;
      }
    },
    getCustomerList() {
      customerApi.getAll().then((response) => {
        let customerList = [];
        response.data.forEach(element => {
          customerList.push({
            value: element.id,
            label: element.tag 
          });
        });
        this.customerList = customerList;
      }).catch(error => {

      });
    }
  }
};
</script>
